package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cderp.mapper.BaseDataSupplierMapper;
import com.ruoyi.cderp.domain.BaseDataSupplier;
import com.ruoyi.cderp.service.IBaseDataSupplierService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 供应商Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-18
 */
@Service
public class BaseDataSupplierServiceImpl implements IBaseDataSupplierService
{
    @Autowired
    private BaseDataSupplierMapper baseDataSupplierMapper;

    LoginUser loginUser = null;

    /**
     * 查询供应商
     *
     * @param id 供应商主键
     * @return 供应商
     */
    @Override
    public BaseDataSupplier selectBaseDataSupplierById(String id)
    {
        return baseDataSupplierMapper.selectBaseDataSupplierById(id);
    }

    /**
     * 查询供应商列表
     *
     * @param baseDataSupplier 供应商
     * @return 供应商
     */
    @Override
    public List<BaseDataSupplier> selectBaseDataSupplierList(BaseDataSupplier baseDataSupplier)
    {
        return baseDataSupplierMapper.selectBaseDataSupplierList(baseDataSupplier);
    }

    /**
     * 新增供应商
     *
     * @param baseDataSupplier 供应商
     * @return 结果
     */
    @Override
    public int insertBaseDataSupplier(BaseDataSupplier baseDataSupplier)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String formattedDate = sdf.format(new Date());
        Random random = new Random();
        int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
        baseDataSupplier.setId("10" + formattedDate+randomNumber);
        baseDataSupplier.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        baseDataSupplier.setCreateById(loginUser.getUser().getUserId().toString());
        baseDataSupplier.setCreateBy(loginUser.getUser().getUserName());
        baseDataSupplier.setCreateTime(DateUtils.getNowDate());
        return baseDataSupplierMapper.insertBaseDataSupplier(baseDataSupplier);
    }

    /**
     * 修改供应商
     *
     * @param baseDataSupplier 供应商
     * @return 结果
     */
    @Override
    public int updateBaseDataSupplier(BaseDataSupplier baseDataSupplier)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        baseDataSupplier.setUpdateById(loginUser.getUser().getUserId().toString());
        baseDataSupplier.setUpdateBy(loginUser.getUser().getUserName());
        baseDataSupplier.setUpdateTime(DateUtils.getNowDate());
        return baseDataSupplierMapper.updateBaseDataSupplier(baseDataSupplier);
    }

    /**
     * 批量删除供应商
     *
     * @param ids 需要删除的供应商主键
     * @return 结果
     */
    @Override
    public int deleteBaseDataSupplierByIds(String[] ids)
    {
        return baseDataSupplierMapper.deleteBaseDataSupplierByIds(ids);
    }

    /**
     * 删除供应商信息
     *
     * @param id 供应商主键
     * @return 结果
     */
    @Override
    public int deleteBaseDataSupplierById(String id)
    {
        return baseDataSupplierMapper.deleteBaseDataSupplierById(id);
    }
}
